﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using OutlookHub.Models;
using Silvermoon.UI;

namespace OutlookHub.Pages
{
    public class TaskSortOrderPage : Page
    {
        public class TaskOrderEventArgs : EventArgs
        {
            public TaskOrder TaskOrder { get; private set; }

            public TaskOrderEventArgs(TaskOrder order)
                : base()
            {
                this.TaskOrder = order;
            }
        }

        private ListView listView;
        private ListViewController<GroupPickerItem, TaskOrder> controller;

        public TaskSortOrderPage()
            : base()
        {
            Title = "CHOOSE SORT ORDER";
            SetTransition(300, TransitionMask.Fade, 300, TransitionMask.Fade);
            listView = new ListView { Margin = Margin.Zero };
            listView.TapItem += new EventHandler<ItemEventArgs>(listView_TapItem);
            Controls.Add(listView);
            controller = new ListViewController<GroupPickerItem, TaskOrder>(listView);
            controller.UpdateItem += new EventHandler<ItemControlGeneratorEventArgs<GroupPickerItem>>(controller_UpdateItem);
            controller.DataSource = GetData();
        }

        void listView_TapItem(object sender, ItemEventArgs e)
        {
            Item item = e.Item;
            TaskOrder order = (TaskOrder)item.DataValue;
            if (Selected != null) Selected(this, new TaskOrderEventArgs(order));
            GoBack();
        }

        private IEnumerable<TaskOrder> GetData()
        {
            yield return TaskOrder.DueDate;
            yield return TaskOrder.StartDate;
            yield return TaskOrder.Subject;
            yield return TaskOrder.Priority;
        }

        void controller_UpdateItem(object sender, ItemControlGeneratorEventArgs<GroupPickerItem> e)
        {
            e.Item.Text = ((TaskOrder)e.Value).ToString();
        }

        public event EventHandler<TaskOrderEventArgs> Selected;
    }

}
